<?php
class MaterialSearch extends CFormModel {
	public $categoryId;
	public $name;
	//	public $tagId;
	public function rules() {
		return array(
			array(
				'categoryId',
				'numerical',
				'integerOnly' => true,
			),
			array(
				'name',
				'length',
				'max' => 255
			),
		);
	}

	public function attributeLabels() {
		return array(
			'categoryId' => '类别',
			'name' => '名称',
		);
	}

	/**
	 * @return CDbCriteria
	 */
	public function createCriteria() {
		$criteria = new CDbCriteria;
		$criteria->with = array(
			'altNames',
		);
		$criteria->together = true;
		$criteria->distinct = true;
		if ($this->categoryId) {
			$criteria->compare('categoryId', $this->categoryId);
		}
		if ($this->name) {
			$nameCriteria = new CDbCriteria();
			$nameCriteria->compare('t.name', $this->name, true, 'OR');
			$nameCriteria->compare('altNames.name', $this->name, true, 'OR');
			$criteria->mergeWith($nameCriteria);
		}

		$criteria->order = 't.name ASC';

		return $criteria;
	}
}
